<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="异常单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['abnormalCode']" placeholder="请输入异常单号"></a-input>
        </a-form-item>
        <a-form-item label="制令单" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['commandBill']" placeholder="请输入制令单"></a-input>
        </a-form-item>
        <a-form-item label="机种料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['machineCode']" placeholder="请输入机种料号"></a-input>
        </a-form-item>
        <a-form-item label="机种名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['machineName']" placeholder="请输入机种名称"></a-input>
        </a-form-item>
        <a-form-item label="机种规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['machineGague']" placeholder="请输入机种规格"></a-input>
        </a-form-item>
        <a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['client']" placeholder="请输入客户"></a-input>
        </a-form-item>
        <a-form-item label="检验批号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['inspectBatch']" placeholder="请输入检验批号"></a-input>
        </a-form-item>
        <a-form-item label="送检数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['sendNum']" placeholder="请输入送检数量"></a-input>
        </a-form-item>
        <a-form-item label="检验数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['inspectNum']" placeholder="请输入检验数量"></a-input>
        </a-form-item>
        <a-form-item label="不良数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['badNum']" placeholder="请输入不良数量"></a-input>
        </a-form-item>
        <a-form-item label="生产区域" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['produceRegion']" placeholder="请输入生产区域"></a-input>
        </a-form-item>
        <a-form-item label="异常类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['abnormalType']" placeholder="请输入异常类型"></a-input>
        </a-form-item>
        <a-form-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['appendix']" :trigger-change="true"></j-upload>
        </a-form-item>
        <a-form-item label="异常描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['abnormalContent']" rows="4" placeholder="请输入异常描述"/>
        </a-form-item>
        
      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JUpload from '@/components/jeecg/JUpload'
  
  export default {
    name: "MesQualityAbnormalModal",
    components: { 
      JUpload,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/inspect/mesQualityAbnormal/add",
          edit: "/inspect/mesQualityAbnormal/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'abnormalCode','commandBill','machineCode','machineName','machineGague','client','inspectBatch','sendNum','inspectNum','badNum','produceRegion','abnormalType','appendix','abnormalContent'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'abnormalCode','commandBill','machineCode','machineName','machineGague','client','inspectBatch','sendNum','inspectNum','badNum','produceRegion','abnormalType','appendix','abnormalContent'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>